package cn.wolfcode.p2p.mgrsite.controller;

import java.text.DateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.business.query.BidRequestQueryObject;
import cn.wolfcode.p2p.business.service.IBidRequestService;

/**
 * 后台借款相关的控制器
 * 
 * @author wolfcode
 *
 */
@Controller
public class BidRequestController {

	@Autowired
	private IBidRequestService bidRequestService;

	/**
	 * 发表前审核列表
	 */
	@RequestMapping("bidrequest_publishaudit_list")
	public String publishAuditList(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
		qo.setState(Constants.BIDREQUEST_STATE_APPLY);
		model.addAttribute("pageResult", this.bidRequestService.query(qo));
		return "bidrequest/publish_audit";
	}

	/**
	 * 风控终审列表
	 */
	@RequestMapping("bidrequest_audit1_list")
	public String audit1List(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
		qo.setState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
		model.addAttribute("pageResult", this.bidRequestService.query(qo));
		return "bidrequest/audit1";
	}

	/**
	 * 财务终审列表
	 */
	@RequestMapping("bidrequest_audit2_list")
	public String audit2List(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
		qo.setState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
		model.addAttribute("pageResult", this.bidRequestService.query(qo));
		return "bidrequest/audit2";
	}

	/**
	 * 发表前审核
	 */
	@RequestMapping("bidrequest_publishaudit")
	@ResponseBody
	public JSONResult publishAudit(Long id, int state, String remark,
			@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date publishTime) {
		JSONResult result = new JSONResult();
		try {
			this.bidRequestService.publishAudit(id, state, remark, publishTime);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 风控终审审核
	 */
	@RequestMapping("bidrequest_audit1")
	@ResponseBody
	public JSONResult fullAudit1(Long id, int state, String remark) {
		JSONResult result = new JSONResult();
		try {
			this.bidRequestService.fullAudit1(id, state, remark);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 财务终审审核
	 */
	@RequestMapping("bidrequest_audit2")
	@ResponseBody
	public JSONResult fullAudit2(Long id, int state, String remark) {
		JSONResult result = new JSONResult();
		try {
			this.bidRequestService.fullAudit2(id, state, remark);
		} catch (DisplayableException ex) {
			result = new JSONResult(false, ex.getMessage());
		} catch (Exception e) {
			// 把错误信息记录到服务器中日志文件中,方便排查问题.
			result = new JSONResult(false, "系统异常,请联系管理员!");
			e.printStackTrace();
		}
		return result;
	}

}
